package cbs.jdtag;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import cbs.jdtag.DataModel2.Token;

public class TokenLabel2 extends JPanel {

	private static final long serialVersionUID = 7652782745984464865L;
	
	public static int WIDTH = 250;
	public static int HEIGHT = 15;
	
	private Token token;
	private JLabel lineNumberLabel;
	private JLabel textLabel;
	private JLabel nodeLabel;
	
	public TokenLabel2(Token token) {
		this(token, false);
	}
	
	public TokenLabel2(Token token, final boolean posLeft) {
		
		this.token = token;
		this.lineNumberLabel = new JLabel(token.lineNumber + ".");
		this.textLabel = new JLabel(token.text);
		this.nodeLabel = new JLabel(" " + token.key + token.id + " ");
		token.tokenLabel = this;
		
		final Color borderColor = Color.WHITE;
//		if (AppContext.getDM().isShowBoxes()) {
//			lineNumberLabel.setBorder(BorderFactory.createLineBorder(borderColor));
//			textLabel.setBorder(BorderFactory.createLineBorder(borderColor));
			nodeLabel.setBorder(BorderFactory.createLineBorder(borderColor));
//		}
		
		setOpaque(false);
		setSize(WIDTH, HEIGHT);
		lineNumberLabel.setMinimumSize(new Dimension(WIDTH/6, lineNumberLabel.getMinimumSize().height));
		nodeLabel.setMinimumSize(new Dimension(WIDTH/5, nodeLabel.getMinimumSize().height));
		setLocation(token.x, token.y);

		setLayout(new GridBagLayout());
		if (posLeft) {
			nodeLabel.setHorizontalAlignment(JLabel.LEFT);
			textLabel.setHorizontalAlignment(JLabel.LEFT);
			lineNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
			add(nodeLabel, getGbc(0, 0.0f, 0, 0));
			add(textLabel, getGbc(1, 1.0f, 5, 5));
			add(lineNumberLabel, getGbc(2, 0.0f, 0, 0));
		}
		else {
			nodeLabel.setHorizontalAlignment(JLabel.RIGHT);
			textLabel.setHorizontalAlignment(JLabel.RIGHT);
			lineNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
			add(lineNumberLabel, getGbc(0, 0.0f, 0, 0));
			add(textLabel, getGbc(1, 1.0f, 5, 5));
			add(nodeLabel, getGbc(2, 0.0f, 0, 0));
		}
	}
	
	public Token getToken() {
		return token;
	}
	
	public void setHighlighted(final boolean highlighted) {
		token.highlighted = highlighted;
		if (highlighted) {
			lineNumberLabel.setForeground(Color.BLUE);
			textLabel.setForeground(Color.BLUE);
			nodeLabel.setForeground(Color.BLUE);
		}
		else {
			lineNumberLabel.setForeground(Color.BLACK);
			textLabel.setForeground(Color.BLACK);
			nodeLabel.setForeground(Color.BLACK);
		}
	}
	
	private GridBagConstraints getGbc(int gridx, float weightx,
			int insetLeft, int insetRight) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = weightx;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = gridx;
		gbc.gridy = 0;
		gbc.insets = new Insets(0, insetLeft, 0, insetRight);
		return gbc;
	}
	
	/**
	 * Fills up the nr string if the number is shorter
	 * than 3 places 
	 * 
	 * @param nr
	 * @return
	 */
	private String getNrString(final int nr) {
		// MAX LENGTH IS 5
		
		if (nr == 0) {
			return "__0.";
		}
		else if (nr < 0) {
			return "ERROR";
		}
		
		// TODO ensure equal width for arbitrary numbers, i.e. __1_ and _10_ not _1_ and _10_
		int nrLength = (int) (Math.log10(nr) + 1);
		
		StringBuffer sb = new StringBuffer(5);
		sb.append(" ");
		for (int i = 0; i < 3 - nrLength; i++) {
			sb.append("_");
		}
		sb.append(nr);
		sb.append(".");
		
		return sb.toString();
	}

//	public boolean isHighlighted() {
//		return token.highlighted;
//	}
}

